package logic

import (
	"context"
	"fmt"
	"week1/models/mysql"

	"week1/goodrpc/goodrpc"
	"week1/goodrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodAddLogic {
	return &GoodAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodAddLogic) GoodAdd(in *goodrpc.GoodAddRequest) (*goodrpc.GoodAddResponse, error) {
	// todo: add your logic here and delete this line

	var findUser mysql.User
	data, err := findUser.FindUserById(in.UserId)
	if err != nil {
		return nil, err
	}
	if data.RealName == "" {
		return nil, fmt.Errorf("该用户还未实名")
	}
	var good mysql.Good
	good = mysql.Good{
		Title:  in.Title,
		Price:  float64(in.Price),
		Stock:  int(in.Stock),
		Info:   in.Info,
		UserId: int(in.UserId),
	}
	mysql.DB.Debug().Create(&good)
	return &goodrpc.GoodAddResponse{
		GoodId: int64(good.ID),
	}, nil
}
